package cc.fozone.note.view.handler;

import cc.fozone.note.help.Boundary;
import cc.fozone.note.help.TextHelper;
import cc.fozone.note.service.IArticleService;
import cc.fozone.note.service.ITemplateService;
import cc.fozone.note.view.ContentView;
import cc.fozone.note.view.EditorView;
import cc.fozone.note.view.OutputView;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.IndexRange;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

@SuppressWarnings("deprecation")
public class SaveEventHandler<T extends Event> implements EventHandler<Event> {
	private ContentView contentView;
	private IArticleService articleService;
	private ITemplateService templateService;
	private EditorView editor;
	private OutputView output;

	public SaveEventHandler(ContentView contentView, EditorView editor,
			OutputView output, IArticleService articleService,
			ITemplateService templateService) {
		super();
		this.contentView = contentView;
		this.articleService = articleService;
		this.templateService = templateService;
		this.editor = editor;
		this.output = output;
	}

	@Override
	public void handle(Event event) {
		// TODO Auto-generated method stub
		if (event instanceof KeyEvent) {
			KeyEvent keyEvent = (KeyEvent) event;
			KeyCode code = keyEvent.getCode();
			if (keyEvent.isControlDown()) {
				if (code == KeyCode.S) {
					doHandleSave();
				} else if (code == KeyCode.N) {
					contentView.resetContent();
				} else if (code == KeyCode.M) {
					doHandleHtml();
				} else if (code == KeyCode.D) {
					doHandleDeleteLine(event);
				} else if (code == KeyCode.Z){
					doHandleUndo(event);
				} else if (code == KeyCode.Y){
					
				} else if (code == KeyCode.P){
					doHandlePreview();
				}
			}
		} else if (event instanceof ActionEvent) {
			doHandleSave();
		}
	}

	/**
	 * 保存并预览文章
	 */
	private void doHandleSave() {
		try {
			String text = editor.getText();
			String title = editor.getTitle();
			String result = templateService.convertToHTML(title, text);
			output.loadContent(result);

			editor.updateArticle();
			articleService.save(editor.getArticle());

		} catch (Exception e) {

		}
	}
	
	private void doHandlePreview(){
		try{
			String text = editor.getText();
			String title = editor.getTitle();
			String html = templateService.convertToHTML(title, text);
			output.loadContent(html);
		}catch(Exception e){
			
		}
	}

	private void doHandleHtml() {
		String text = editor.getText();
		String title = editor.getTitle();
		String result = templateService.convertMarkdown(text);
		result = "<h2>" + title + "</h2>\n" + result;
//		Dialogs.create().title("Markdown HTML")
//				.styleClass(Dialog.STYLE_CLASS_CROSS_PLATFORM)
//				.graphic(new TextArea(result)).showInformation();
	}

	/**
	 * 删除编辑器一行代码，保持与Eclipse风格统一
	 * 1. 还需要将删除的内容放置到UNDO栈中
	 * @param event
	 */
	private void doHandleDeleteLine(Event event) {
		TextArea textArea = (TextArea) event.getSource();
		IndexRange range = textArea.getSelection();
		String text = textArea.getText();

		Boundary boundary = TextHelper.transferLine(text, range.getStart(),
				range.getEnd());
		
		int start = boundary.getStart(), end = boundary.getEnd() , next = start+1;
		
		textArea.deleteText(start,end);
		textArea.selectRange(next,next);
	}
	
	private void doHandleUndo(Event event){
		TextArea textArea = (TextArea) event.getSource();
		System.out.println(textArea.getPrefRowCount());
	}
}